/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ModuloDiagnosticador;

import ModuloBaseDelConocimiento.Caso;
import ModuloBaseDelConocimiento.Enfermedad;
import ModuloControladorBD.EnfermedadJpaController;
import java.util.*;
import javax.swing.JOptionPane;

/**
 *
 * @author rayner
 */
public class LVQ {
    private int numeroDeNeuronas ;
    private double t_aprendizaje;
    private ArrayList <NeuronaLVQ> neuronas;
    public LVQ(double apren, int numeroDeEntradas){
        t_aprendizaje = apren;
        neuronas = new ArrayList<NeuronaLVQ>();
        EnfermedadJpaController enf_cont = new EnfermedadJpaController();
        numeroDeNeuronas = enf_cont.getEnfermedadCount();
        List<Enfermedad> enfermedades = enf_cont.findEnfermedadEntities();
        for(int i = 0; i < numeroDeNeuronas; i ++){
            NeuronaLVQ neurona = new NeuronaLVQ(i, numeroDeEntradas, enfermedades.get(i));
            neuronas.add(neurona);
        }
    }
    public void actualizarApren(){
        t_aprendizaje /= 2.0;
    }
    public void entrenarRedLVQ1(double input[], int target, String desp){
        double dist_winner = (double)(1 << 30);
        int neurona_winner = -1;
        for(int i = 0; i < numeroDeNeuronas ; i ++){
            if(neuronas.get(i).isSameClass(target)){
                if(neuronas.get(i).getAreZeros()){
                   // JOptionPane.showMessageDialog(null, target);
                    neuronas.get(i).updatePesos(true, 1.0 , input, desp);
                    return;
                }
            }
            if(!neuronas.get(i).getAreZeros()){
                double dist_cand = neuronas.get(i).calcularDistEuclidiana(input);
                if(dist_cand < dist_winner){
                    dist_cand = dist_winner;
                    neurona_winner = i;
                }
            }
        }
        //JOptionPane.showMessageDialog(null, target + " " + dist_winner + " " + neurona_winner);
        if(neuronas.get(neurona_winner).isSameClass(target)){
            neuronas.get(neurona_winner).updatePesos(true, t_aprendizaje, input, desp);
        }else neuronas.get(neurona_winner).updatePesos(false, t_aprendizaje, input, "");
    }
    public Caso recuperarMejorCaso(double input[]){
        double dist_winner = (double)(1000000000);
        int neurona_winner = -1;
        for(int i = 0; i < numeroDeNeuronas ; i ++){
            if(neuronas.get(i).getAreZeros()) continue;
            double dist_cand = neuronas.get(i).calcularDistEuclidiana(input);
            
            if(dist_cand < dist_winner){
                
                dist_winner = dist_cand;
                neurona_winner = i;
            }
        }
        JOptionPane.showMessageDialog(null,  neurona_winner + " " + dist_winner + " " + neuronas.get(neurona_winner).getCaso().getEnfermedad().getNombre());
        return neuronas.get(neurona_winner).getCaso();
    }
}
